import scala.actors._

/**
 * Holds Passengers that failed at either of the Scans until the end of the day
 * where they are transferred to "permanent detention" (the driver)
 * @author Samuel Nelson, Robert Vrooman, James Fitzgerald, Gabriel Marcano
 */
class Jail extends Actor{
  var prisoners = List[Passenger]()
  var starts = 0
  def act(){
    loop{
      react{
        case Startup =>
          starts += 1
          if(starts == 1){
            println("Jail has received Startup.")
          }
          else{
            println("Jail has received another Startup.")
          } //Swallow message since Jail is already started
        case passenger : Passenger =>
          println("Jail has received Passenger " + passenger.ID + ".")
          prisoners ::= passenger
	        
        case Shutdown =>
          println("Jail has received Shutdown.")
          shutdownRequested
      }
    }
  }
	
  // reactWithin(0) gives a TIMEOUT as soon as the mailbox is empty
  def shutdownRequested : Nothing = reactWithin(0){
    case Shutdown =>{
        println("Jail has received another Shutdown.")
        shutdownRequested
      }
    case TIMEOUT =>{
        for(i <- 0 to prisoners.length - 1){
          println("Passenger " + prisoners(i).ID +
                  " has been transferred to permanent detention.")
        }
        println("Jail has now shutdown.")
        starts = 0;
        exit
      }
  }
}